package com.metel.mservice.common.utils;



import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.metel.mservice.common.constants.HttpStatus;
import com.metel.mservice.common.exception.CommonServiceException;
import com.metel.mservice.common.faultcode.ChatONOpenAPIFaultCode;
import com.metel.mservice.common.faultcode.message.ErrorMessage;


public class PhoneNumberUtil {

	private static PhoneNumberUtil instance;
	
	
	private List<String> CountryCodes;
	private List<String> CountryCallingCodes;
	private List<String> InternationalCallPrefix;
	private List<Pattern> CountryPattern;
	private List<Boolean> DelStartZero;
	
	

	public PhoneNumberUtil() {

		
		
		
		String input = 
			"WEB	0\n"
			+"WEB	77\n"
			+"Afghanistan	93\n"
			+ "Albania	355\n"
			+ "Algeria	213\n"
			+ "American Samoa	1\n"
			+ "Andorra	376\n"
			+ "Angola	244\n"
			+ "Anguilla	1\n"
			+ "Antigua and Barbuda	1\n"
			+ "Argentina 25 de Mayo (Bs As)	5492345\n"
			+ "Argentina 28 de Noviembre	5492902\n"
			+ "Argentina 9 de Julio (Bs As)	5492317\n"
			+ "Argentina Acebal	5493469\n"
			+ "Argentina Adelia Mar챠a	5493585\n"
			+ "Argentina Aguilares	5493865\n"
			+ "Argentina Aimogasta	5493827\n"
			+ "Argentina Alcaraz	5493438\n"
			+ "Argentina Alcorta	5493465\n"
			+ "Argentina Alejandro Korn	5492225\n"
			+ "Argentina Allen	5492941\n"
			+ "Argentina Almafuerte (C처rdoba)	5493571\n"
			+ "Argentina Alta Gracia	5493547\n"
			+ "Argentina Amaicha del Valle	5493892\n"
			+ "Argentina Am챕rica	5492337\n"
			+ "Argentina A챰atuya	5493844\n"
			+ "Argentina Andalgala	5493835\n"
			+ "Argentina Anillaco	5493827\n"
			+ "Argentina Ap처stoles	5493758\n"
			+ "Argentina Arguello	5493543\n"
			+ "Argentina Arrecifes	5492478\n"
			+ "Argentina Arribe챰os	5492353\n"
			+ "Argentina Arroyito (C처rdoba)	5493576\n"
			+ "Argentina Arroyo Seco	5493402\n"
			+ "Argentina Arrufo	5493491\n"
			+ "Argentina Ascenci처n	5492353\n"
			+ "Argentina Ayacucho	5492296\n"
			+ "Argentina Azul	5492281\n"
			+ "Argentina Bah챠a Blanca	549291\n"
			+ "Argentina Balcarce	5492266\n"
			+ "Argentina Ballesteros	549353\n"
			+ "Argentina Balnearia	5493563\n"
			+ "Argentina Bandera	5493857\n"
			+ "Argentina Baradero	5493329\n"
			+ "Argentina Barrancas (Santa Fe)	5493466\n"
			+ "Argentina Barreal	5492648\n"
			+ "Argentina Barrio Los Pescadores	5493722\n"
			+ "Argentina Bell Ville	5493534\n"
			+ "Argentina Benito Juarez	5492292\n"
			+ "Argentina Bernardo de Irigoyen	5493741\n"
			+ "Argentina Bernasconi	5492925\n"
			+ "Argentina Bolivar	5492314\n"
			+ "Argentina Bouchard	5493387\n"
			+ "Argentina Bovril	5493438\n"
			+ "Argentina Bragado	5492342\n"
			+ "Argentina Buena Esperanza	5492658\n"
			+ "Argentina Burruyacu	5493894\n"
			+ "Argentina Caa Cati	5493781\n"
			+ "Argentina Cafayate	5493868\n"
			+ "Argentina Caleta Olivia	549297\n"
			+ "Argentina Caleufu	5492335\n"
			+ "Argentina Calingasta	5492648\n"
			+ "Argentina Campana	5493489\n"
			+ "Argentina Campo Gallo	5493846\n"
			+ "Argentina Ca챰ada de G처mez	5493471\n"
			+ "Argentina Canals	5493463\n"
			+ "Argentina Candelaria (Misiones)	5493752\n"
			+ "Argentina Candelaria (San Luis)	5492651\n"
			+ "Argentina Ca챰uelas	5492226\n"
			+ "Argentina Capilla del Se챰or	5492323\n"
			+ "Argentina Capital Federal (AMBA)	54911\n"
			+ "Argentina Capit찼n Sarmiento	5492478\n"
			+ "Argentina Carcara챰a	549341\n"
			+ "Argentina Carhue	5492936\n"
			+ "Argentina Carlos Casares	5492395\n"
			+ "Argentina Carlos Spegazzini	5492274\n"
			+ "Argentina Carlos Tejedor	5492357\n"
			+ "Argentina Carmen de Areco	5492273\n"
			+ "Argentina Casbas	5492929\n"
			+ "Argentina Casilda	5493464\n"
			+ "Argentina Castelli	5492245\n"
			+ "Argentina Catamarca	5493833\n"
			+ "Argentina Caucete	549264\n"
			+ "Argentina Ceres	5493491\n"
			+ "Argentina Chacabuco	5492352\n"
			+ "Argentina Chaj찼n	5493582\n"
			+ "Argentina Chajar챠	5493456\n"
			+ "Argentina Chamical	5493826\n"
			+ "Argentina Charadai	5493721\n"
			+ "Argentina Charat찼	5493731\n"
			+ "Argentina Chascom첬s	5492241\n"
			+ "Argentina Chepes	5493821\n"
			+ "Argentina Chilecito	5493825\n"
			+ "Argentina Chivilcoy	5492346\n"
			+ "Argentina Choele Choel	5492946\n"
			+ "Argentina Chos Malal	5492948\n"
			+ "Argentina Cinco Saltos	549299\n"
			+ "Argentina Claromeco	5492982\n"
			+ "Argentina Clorinda	5493718\n"
			+ "Argentina Col처n (Bs As)	5492473\n"
			+ "Argentina Col처n (Entre R챠os)	5493447\n"
			+ "Argentina Colonia Bar처n	5492333\n"
			+ "Argentina Colonia Ben챠tez	5493722\n"
			+ "Argentina Comandante Fontana	5493716\n"
			+ "Argentina Comandante Luis Piedra Buena	5492962\n"
			+ "Argentina Comandante N. Otamendi	5492291\n"
			+ "Argentina Comodoro Rivadavia	549297\n"
			+ "Argentina Concepci처n (Tucum찼n)	5493865\n"
			+ "Argentina Concepci처n del Uruguay	5493442\n"
			+ "Argentina Concordia	549345\n"
			+ "Argentina C처rdoba	549351\n"
			+ "Argentina Coronda	549342\n"
			+ "Argentina Coronel Brandsen	5492223\n"
			+ "Argentina Coronel Dorrego	5492921\n"
			+ "Argentina Coronel Moldes (C처rdoba)	5493582\n"
			+ "Argentina Coronel Pringles	5492922\n"
			+ "Argentina Coronel Su찼rez	5492926\n"
			+ "Argentina Coronel Vidal	5492265\n"
			+ "Argentina Corral de Bustos	5493468\n"
			+ "Argentina Corrientes	5493783\n"
			+ "Argentina Cosqu챠n	5493541\n"
			+ "Argentina Cruz Alta	5493467\n"
			+ "Argentina Cruz del Eje	5493549\n"
			+ "Argentina Curuz첬 Cuati찼	5493774\n"
			+ "Argentina Cutral Co	549299\n"
			+ "Argentina Daireaux	5492316\n"
			+ "Argentina Darregueira	5492924\n"
			+ "Argentina Dean Funes	5493521\n"
			+ "Argentina Desag횄쩌adero	5492626\n"
			+ "Argentina Dolores (Bs As)	5492245\n"
			+ "Argentina Dudignac	5492317\n"
			+ "Argentina Eduardo Castex	5492334\n"
			+ "Argentina El Calafate	5492902\n"
			+ "Argentina El Dorado	5493751\n"
			+ "Argentina El Sombrero	5493783\n"
			+ "Argentina El Tr챕bol	5493401\n"
			+ "Argentina Embajador Martini	5492335\n"
			+ "Argentina Escobar	5493488\n"
			+ "Argentina Esperanza (Santa Fe)	5493496\n"
			+ "Argentina Esquel	5492945\n"
			+ "Argentina Federal	5493454\n"
			+ "Argentina Firmat	5493465\n"
			+ "Argentina Formosa	5493717\n"
			+ "Argentina Fort챠n Olavarr챠a	5492337\n"
			+ "Argentina Fr챠as	5493854\n"
			+ "Argentina G찼lvez	5493404\n"
			+ "Argentina Garupa	5493752\n"
			+ "Argentina General Acha	5492952\n"
			+ "Argentina General Alvear (Bs As)	5492344\n"
			+ "Argentina General Alvear (Mendoza)	5492625\n"
			+ "Argentina General Arenales	5492353\n"
			+ "Argentina General Belgrano	5492243\n"
			+ "Argentina General Conesa (Bs As)	5492245\n"
			+ "Argentina General Conesa (R챠o Negro)	5492931\n"
			+ "Argentina General Deheza	549358\n"
			+ "Argentina General Guido	5492268\n"
			+ "Argentina General Jos챕 de San Mart챠n	5493725\n"
			+ "Argentina General Lamadrid	5492286\n"
			+ "Argentina General Madariaga	5492267\n"
			+ "Argentina General Mosconi	5493711\n"
			+ "Argentina General Pico	5492302\n"
			+ "Argentina General Pinto	5492356\n"
			+ "Argentina General Pir찼n	5492265\n"
			+ "Argentina General Roca (R챠o Negro)	5492941\n"
			+ "Argentina General Rodr챠guez	549237\n"
			+ "Argentina General Villegas	5493388\n"
			+ "Argentina Glew	5492224\n"
			+ "Argentina Gobernador Crespo	5493498\n"
			+ "Argentina Gonz찼lez Cat찼n	5492202\n"
			+ "Argentina Goya	5493777\n"
			+ "Argentina Gualeguay	5493444\n"
			+ "Argentina Gualeguaych첬	5493446\n"
			+ "Argentina Guamini	5492929\n"
			+ "Argentina Guernica	5492224\n"
			+ "Argentina Huinca Renanco	5492336\n"
			+ "Argentina Humahuaca	5493887\n"
			+ "Argentina Ibarreta	5493716\n"
			+ "Argentina Ingeniero Guillermo N. Ju찼rez	5493711\n"
			+ "Argentina Ingeniero Jacobacci	5492940\n"
			+ "Argentina Ingeniero Luiggi	5492335\n"
			+ "Argentina Inriville	5493467\n"
			+ "Argentina Ituzaing처	5493786\n"
			+ "Argentina Jachal	5492647\n"
			+ "Argentina Jes첬s Mar챠a	5493525\n"
			+ "Argentina Joaqu챠n V. Gonz찼lez	5493877\n"
			+ "Argentina Jos챕 C. Paz	5492320\n"
			+ "Argentina Juan B. Alberdi	5492354\n"
			+ "Argentina Juan Mar챠a Guti챕rrez	5492229\n"
			+ "Argentina Juan N. Fern찼ndez	5492264\n"
			+ "Argentina Jun챠n (Bs As)	5492362\n"
			+ "Argentina Jun챠n de los Andes	5492972\n"
			+ "Argentina La Carlota	5493584\n"
			+ "Argentina La Dormida	5492623\n"
			+ "Argentina La Dulce	5492264\n"
			+ "Argentina La Falda	5493548\n"
			+ "Argentina La Paz (Entre R챠os)	5493437\n"
			+ "Argentina La Paz (Mendoza)	5492626\n"
			+ "Argentina La Plata	549221\n"
			+ "Argentina La Puerta (C처rdoba)	5493575\n"
			+ "Argentina La Quiaca	5493885\n"
			+ "Argentina La Rioja	5493822\n"
			+ "Argentina La Toma	5492655\n"
			+ "Argentina Laboulaye	5493385\n"
			+ "Argentina Laguna Brava	5493783\n"
			+ "Argentina Lamadrid	5493891\n"
			+ "Argentina Laprida	5492285\n"
			+ "Argentina Las Armas	5492268\n"
			+ "Argentina Las Flores (Bs As)	5492244\n"
			+ "Argentina Las Lajitas	5493877\n"
			+ "Argentina Las Lomitas	5493715\n"
			+ "Argentina Las Toscas (Santa Fe)	5493482\n"
			+ "Argentina Las Varillas	5493533\n"
			+ "Argentina Leandro N. Alem (Misiones)	5493754\n"
			+ "Argentina Leones	5493472\n"
			+ "Argentina Lezama	5492242\n"
			+ "Argentina Libertad	549220\n"
			+ "Argentina Libertador General San Mart챠n	5493886\n"
			+ "Argentina Lincoln	5492355\n"
			+ "Argentina Llambi Campbell	5493497\n"
			+ "Argentina Lober챠a	5492261\n"
			+ "Argentina Lobos	5492227\n"
			+ "Argentina Loncopue	5492948\n"
			+ "Argentina L처pez Camelo	5493327\n"
			+ "Argentina Los Cardales	5492322\n"
			+ "Argentina Los Molinos (C처rdoba)	5493547\n"
			+ "Argentina Los Toldos (Bs As)	5492358\n"
			+ "Argentina Luj찼n (Bs As)	5492323\n"
			+ "Argentina Luj찼n de Cuyo	549261\n"
			+ "Argentina Macach챠n	5492953\n"
			+ "Argentina Machagai	5493734\n"
			+ "Argentina Maciel	5493476\n"
			+ "Argentina Magdalena	5492221\n"
			+ "Argentina Maip첬 (Bs As)	5492268\n"
			+ "Argentina Maip첬 (Mendoza)	549261\n"
			+ "Argentina Malargue	5492627\n"
			+ "Argentina Mar de Aj처	5492257\n"
			+ "Argentina Mar del Plata	549223\n"
			+ "Argentina Marco Paz	549220\n"
			+ "Argentina Marcos Ju찼rez	5493472\n"
			+ "Argentina Margarita Bel챕n	5493722\n"
			+ "Argentina M챕danos (Bs As)	5492927\n"
			+ "Argentina Mendoza	549261\n"
			+ "Argentina Mercedes (Misiones)	5493773\n"
			+ "Argentina Mercedes (San Luis)	5492657\n"
			+ "Argentina Merlo (Bs As)	549220\n"
			+ "Argentina Merlo (San Luis)	5492656\n"
			+ "Argentina Met찼n	5493876\n"
			+ "Argentina Miramar (Bs As)	5492291\n"
			+ "Argentina Mois챕s Ville	5493409\n"
			+ "Argentina Moj처n de Fierro	5493717\n"
			+ "Argentina Monte	5492271\n"
			+ "Argentina Monte Caseros	5493775\n"
			+ "Argentina Monte Hermoso	5492921\n"
			+ "Argentina Monte Quemado	5493841\n"
			+ "Argentina Monteagudo	5493891\n"
			+ "Argentina Monteros	5493863\n"
			+ "Argentina Moreno	549237\n"
			+ "Argentina Morteros	5493562\n"
			+ "Argentina Naschel	5492656\n"
			+ "Argentina Navarro	5492272\n"
			+ "Argentina Necochea	5492262\n"
			+ "Argentina Neuqu챕n	549299\n"
			+ "Argentina Nogoy찼	5493435\n"
			+ "Argentina Nonogasta	5493825\n"
			+ "Argentina Norberto de la Riestra	5492343\n"
			+ "Argentina Nueva Esperanza	5493861\n"
			+ "Argentina O' Brien	5492342\n"
			+ "Argentina Obera	5493755\n"
			+ "Argentina Ojo de Agua (Santiago del Estero)	5493856\n"
			+ "Argentina Olavarr챠a	5492284\n"
			+ "Argentina Oliva	5493532\n"
			+ "Argentina Oncativo	5493572\n"
			+ "Argentina Oran	5493878\n"
			+ "Argentina Orense	5492982\n"
			+ "Argentina Paran찼	549343\n"
			+ "Argentina Paso de los Libres	5493772\n"
			+ "Argentina Pedro Luro	5492928\n"
			+ "Argentina Pehuajo	5492396\n"
			+ "Argentina Pergamino	5492477\n"
			+ "Argentina Perito Moreno	5492963\n"
			+ "Argentina Pig횄쩌e	5492923\n"
			+ "Argentina Pila	5492242\n"
			+ "Argentina Pilar (Bs As)	5492322\n"
			+ "Argentina Pinamar	5492254\n"
			+ "Argentina Playa Chapadmalal	549223\n"
			+ "Argentina Posadas	5493752\n"
			+ "Argentina Presidencia De La Plaza	5493734\n"
			+ "Argentina Presidencia Roque S찼enz Pe챰a	5493732\n"
			+ "Argentina Puerto Bastiani	5493722\n"
			+ "Argentina Puerto Deseado	549297\n"
			+ "Argentina Puerto Iguaz첬	5493757\n"
			+ "Argentina Puerto Madryn	5492965\n"
			+ "Argentina Puerto Rico	5493743\n"
			+ "Argentina Puerto Santa Cruz	5492962\n"
			+ "Argentina Puerto Tirol	5493722\n"
			+ "Argentina Punta Alta	5492932\n"
			+ "Argentina Quemu Quemu	5492333\n"
			+ "Argentina Quimili	5493843\n"
			+ "Argentina Quines	5492651\n"
			+ "Argentina Quiroga	5492317\n"
			+ "Argentina Rafael Obligado	5492475\n"
			+ "Argentina Rafaela	5493492\n"
			+ "Argentina Ramallo	5493407\n"
			+ "Argentina Ranchillos	5493869\n"
			+ "Argentina Rapelli	5493861\n"
			+ "Argentina Rauch	5492297\n"
			+ "Argentina Rawson (Chubut)	5492965\n"
			+ "Argentina Realico	5492331\n"
			+ "Argentina Reconquista	5493482\n"
			+ "Argentina Recreo (Catamarca)	5493832\n"
			+ "Argentina Resistencia	5493722\n"
			+ "Argentina Riachuelo	5493783\n"
			+ "Argentina R챠o Ceballos	5493543\n"
			+ "Argentina R챠o Colorado (R챠o Negro)	5492931\n"
			+ "Argentina R챠o Cuarto	549358\n"
			+ "Argentina R챠o Gallegos	5492966\n"
			+ "Argentina R챠o Grande (Tierra del Fuego)	5492964\n"
			+ "Argentina R챠o Mayo	5492903\n"
			+ "Argentina R챠o Primero	5493574\n"
			+ "Argentina R챠o Segundo	5493572\n"
			+ "Argentina R챠o Tercero	5493571\n"
			+ "Argentina R챠o Turbio	5492902\n"
			+ "Argentina Rivera	5492935\n"
			+ "Argentina Rojas	5492475\n"
			+ "Argentina Rosario	549341\n"
			+ "Argentina Rosario del Tal찼	5493445\n"
			+ "Argentina Rufino	5493382\n"
			+ "Argentina Saladas	5493782\n"
			+ "Argentina Saladillo (Bs As)	5492344\n"
			+ "Argentina Salazar	5492393\n"
			+ "Argentina Salliquelo	5492394\n"
			+ "Argentina Salsacate	5493542\n"
			+ "Argentina Salta	549387\n"
			+ "Argentina Salto	5492474\n"
			+ "Argentina Sampacho	5493582\n"
			+ "Argentina San Agust챠n del Valle F챕rtil	5492646\n"
			+ "Argentina San Andr챕s de Giles	5492325\n"
			+ "Argentina San Antonio de Areco	5492326\n"
			+ "Argentina San Antonio Oeste	5492934\n"
			+ "Argentina San Bernardo (Bs As)	5492257\n"
			+ "Argentina San Carlos Centro	5493404\n"
			+ "Argentina San Carlos de Bariloche	5492944\n"
			+ "Argentina San Clemente del Tuy첬	5492252\n"
			+ "Argentina San Crist처bal	5493408\n"
			+ "Argentina San Francisco (C처rdoba)	5493564\n"
			+ "Argentina San Francisco del Cha챰ar	5493522\n"
			+ "Argentina San Javier (Misiones)	5493754\n"
			+ "Argentina San Javier (Santa Fe)	5493405\n"
			+ "Argentina San Jorge (Santa Fe)	5493406\n"
			+ "Argentina San Jos챕 de Feliciano	5493458\n"
			+ "Argentina San Juan	549264\n"
			+ "Argentina San Juli찼n	5492962\n"
			+ "Argentina San Justo (Santa Fe)	5493498\n"
			+ "Argentina San Lorenzo (Santa Fe)	5493476\n"
			+ "Argentina San Luis (San Luis)	5492651\n"
			+ "Argentina San Luis del Palmar	5493783\n"
			+ "Argentina San Manuel (Bs As)	5492261\n"
			+ "Argentina San Mart챠n (Mendoza)	5492623\n"
			+ "Argentina San Mart챠n (San Juan)	549264\n"
			+ "Argentina San Mart챠n de los Andes	5492972\n"
			+ "Argentina San Miguel de Tucum찼n	549381\n"
			+ "Argentina San Nicol찼s	5493461\n"
			+ "Argentina San Pedro (Buenos Aires)	5493329\n"
			+ "Argentina San Pedro (Jujuy)	5493884\n"
			+ "Argentina San Rafael (Mendoza)	5492627\n"
			+ "Argentina San Salvador de Jujuy	5493788\n"
			+ "Argentina San Vicente (Bs As)	5492225\n"
			+ "Argentina Santa Ana (Corrientes)	5493783\n"
			+ "Argentina Santa Clara del Mar	549223\n"
			+ "Argentina Santa Fe	549342\n"
			+ "Argentina Santa Mar챠a (Catamarca)	5493838\n"
			+ "Argentina Santa Rosa (La Pampa)	5492954\n"
			+ "Argentina Santa Rosa de Calamuchita	5493546\n"
			+ "Argentina Santa Teresa (Santa Fe)	5493460\n"
			+ "Argentina Santa Teresita	5492246\n"
			+ "Argentina Santiago del Estero	549385\n"
			+ "Argentina Santo Tom챕	5493756\n"
			+ "Argentina Sierra Grande	5492934\n"
			+ "Argentina Suipacha	5492324\n"
			+ "Argentina Sumampa	5493856\n"
			+ "Argentina Sunchales	5493493\n"
			+ "Argentina Suncho Corral	5493855\n"
			+ "Argentina Taf챠 del Valle	5493867\n"
			+ "Argentina Tandil	5492293\n"
			+ "Argentina Tapalqu챕	5492283\n"
			+ "Argentina Tartagal (Salta)	5493875\n"
			+ "Argentina Termas de R챠o Hondo	5493858\n"
			+ "Argentina Tilisarao	5492656\n"
			+ "Argentina Tinogasta	5493837\n"
			+ "Argentina Tintina	5493846\n"
			+ "Argentina Tortugas	5493471\n"
			+ "Argentina Trancas	5493862\n"
			+ "Argentina Tr찼nsito	5493576\n"
			+ "Argentina Trelew	5492965\n"
			+ "Argentina Trenque Lauquen	5492392\n"
			+ "Argentina Tres Arroyos	5492983\n"
			+ "Argentina Tres Lomas	5492394\n"
			+ "Argentina Tunuyan	5492622\n"
			+ "Argentina Unquillo	5493543\n"
			+ "Argentina Ushuaia	5492901\n"
			+ "Argentina Uspallata	5492624\n"
			+ "Argentina Vedia	5492354\n"
			+ "Argentina Venado Tuerto	5493462\n"
			+ "Argentina Vera	5493483\n"
			+ "Argentina Ver처nica	5492221\n"
			+ "Argentina Victoria	5493436\n"
			+ "Argentina Vict처rica	5492338\n"
			+ "Argentina Vicu챰a Mackenna	5493583\n"
			+ "Argentina Viedma	5492920\n"
			+ "Argentina Villa 횄혖ngela	5493735\n"
			+ "Argentina Villa Atuel	5492625\n"
			+ "Argentina Villa Carlos Paz	5493541\n"
			+ "Argentina Villa Constituci처n	5493400\n"
			+ "Argentina Villa de Mar챠a de R챠o Seco	5493522\n"
			+ "Argentina Villa del Rosario (C처rdoba)	5493573\n"
			+ "Argentina Villa del Totoral	5493524\n"
			+ "Argentina Villa Dolores	5493544\n"
			+ "Argentina Villa Gesell	5492255\n"
			+ "Argentina Villa Huidobro	5492336\n"
			+ "Argentina Villa Iris	5492925\n"
			+ "Argentina Villa La Angostura	5492944\n"
			+ "Argentina Villa Mar챠a	549353\n"
			+ "Argentina Villa Regina	5492941\n"
			+ "Argentina Villa San Mart챠n	5493845\n"
			+ "Argentina Villaguay	5493455\n"
			+ "Argentina Villalonga	5492928\n"
			+ "Argentina Vivorat찼	5492265\n"
			+ "Argentina Winifreda	5492333\n"
			+ "Argentina Zapala	5492942\n"
			+ "Argentina Z찼rate	5493487\n"
			+ "Argentina	54\n"
			+ "Armenia	374\n"
			+ "Aruba	297\n"
			+ "Ascension	247\n"
			+ "Australia	61	0011\n"
			+ "Australian External Territories	672	0011\n"
			+ "Austria	43\n"
			+ "Azerbaijan	994\n"
			+ "Bahamas	1\n"
			+ "Bahrain	973\n"
			+ "Bangladesh	880\n"
			+ "Barbados	1\n"
			+ "Barbuda	1\n"
			+ "Belarus	375	810\n"
			+ "Belgium	32\n"
			+ "Belize	501\n"
			+ "Benin	229\n"
			+ "Bermuda	1\n"
			+ "Bhutan	975\n"
			+ "Bolivia	591\n"
			+ "Bonaire	599 7\n"
			+ "Bosnia and Herzegovina	387\n"
			+ "Botswana	267\n"
			+ "Brazil	55	00\\d\\d\n"
			+ "British Indian Ocean Territory	246\n"
			+ "British Virgin Islands	1\n"
			+ "Brunei Darussalam	673\n"
			+ "Bulgaria	359\n"
			+ "Burkina Faso	226\n"
			+ "Burundi	257\n"
			+ "Cambodia	855	001|007\n"
			+ "Cameroon	237\n"
			+ "Canada	1\n"
			+ "Cape Verde	238\n"
			+ "Caribbean Netherlands	599\n"
			+ "Cayman Islands	1 345[8]\n"
			+ "Central African Republic	236\n"
			+ "Chad	235\n"
			+ "Chatham Island (New Zealand)	64\n"
			+ "Chile	56	1\\d\\d0\n"
			+ "China	86\n"
			+ "Christmas Island	61\n"
			+ "Cocos (Keeling) Islands	61\n"
			+ "Colombia	57	005|007|009\n"
			+ "Comoros	269\n"
			+ "Congo (Brazzaville)	242\n"
			+ "Congo, The Democratic Republic of the (Zaire)	243\n"
			+ "Cook Islands	682\n"
			+ "Costa Rica	506\n"
			+ "C척te d'Ivoire	225\n"
			+ "Croatia	385\n"
			+ "Cuba	53	119\n"
			+ "Cura챌ao	599\n"
			+ "Cyprus	357\n"
			+ "Czech Republic	420\n"
			+ "Denmark	45\n"
			+ "Diego Garcia	246\n"
			+ "Djibouti	253\n"
			+ "Dominica	1\n"
			+ "Dominican Republic	1\n"
			+ "East Timor	670\n"
			+ "Easter Island	56\n"
			+ "Ecuador	593\n"
			+ "Egypt	20\n"
			+ "El Salvador	503\n"
			+ "Ellipso (Mobile Satellite service)	881\n"
			+ "EMSAT (Mobile Satellite service)	882\n"
			+ "Equatorial Guinea	240\n"
			+ "Eritrea	291\n"
			+ "Estonia	372\n"
			+ "Ethiopia	251\n"
			+ "Falkland Islands (Malvinas)	500\n"
			+ "Faroe Islands	298\n"
			+ "Fiji	679\n"
			+ "Finland	358	990|991|992|994|99500|99511|99532|99533|99541|99551|99555|99559|99577|99588|99590|99599|9977|998|999\n"
			+ "France	33\n"
			+ "French Antilles	596\n"
			+ "French Guiana	594\n"
			+ "French Polynesia	689\n"
			+ "Gabon	241\n"
			+ "Gambia	220\n"
			+ "Georgia	995	00|807|810|811|816|818|854|860|869\n"
			+ "Germany	49\n"
			+ "Ghana	233\n"
			+ "Gibraltar	350\n"
			+ "Global Mobile Satellite System (GMSS)	881\n"
			+ "Greece	30\n"
			+ "Greenland	299\n"
			+ "Grenada	1\n"
			+ "Guadeloupe	590\n"
			+ "Guam	1\n"
			+ "Guatemala	502\n"
			+ "Guernsey	44\n"
			+ "Guinea	224\n"
			+ "Guinea-Bissau	245\n"
			+ "Guyana	592	001\n"
			+ "Haiti	509\n"
			+ "Honduras	504\n"
			+ "Hong Kong	852	001|0060|007|008|009|1666\n"
			+ "Hungary	36\n"
			+ "Iceland	354\n"
			+ "ICO Global (Mobile Satellite Service)	881 0, 881 1\n"
			+ "India	91\n"
			+ "Indonesia	62	001|007|008|009\n"
			+ "Inmarsat SNAC	870\n"
			+ "International Freephone Service	800\n"
			+ "International Shared Cost Service (ISCS)	808\n"
			+ "Iran	98\n"
			+ "Iraq	964\n"
			+ "Ireland	353\n"
			+ "Iridium (Mobile Satellite service)	881 6, 881 7\n"
			+ "Isle of Man	44\n"
			+ "Israel	972	012|013|014|018\n"
			+ "Italy	39\n"
			+ "Jamaica	1\n"
			+ "Japan	81	010\n"
			+ "Jersey	44\n"
			+ "Jordan	962\n"
			+ "Kazakhstan	7	810\n"
			+ "Kenya	254	000|005|006|007\n"
			+ "Kiribati	686\n"
			+ "North Korea	850\n"
			//+ "South Korea	82	001|002|(003\\d\\d)|005|006|(007\\d\\d)|008	[0]?[1,5,7]{1}[0-9]{1}[0-9]{7,8}\n"
			+ "South Korea	82	001|002|(003\\d\\d)|005|006|(007\\d\\d)|008\n"
			+ "Kuwait	965\n"
			+ "Kyrgyzstan	996\n"
			+ "Laos	856\n"
			+ "Latvia	371\n"
			+ "Lebanon	961\n"
			+ "Lesotho	266\n"
			+ "Liberia	231\n"
			+ "Libya	218\n"
			+ "Liechtenstein	423\n"
			+ "Lithuania	370\n"
			+ "Luxembourg	352\n"
			+ "Macau	853\n"
			+ "Macedonia	389\n"
			+ "Madagascar	261\n"
			+ "Malawi	265\n"
			+ "Malaysia	60\n"
			+ "Maldives	960\n"
			+ "Mali	223\n"
			+ "Malta	356\n"
			+ "Marshall Islands	692\n"
			+ "Martinique	596\n"
			+ "Mauritania	222\n"
			+ "Mauritius	230\n"
			+ "Mayotte	262\n"
			+ "Mexico	52\n"
			+ "Micronesia, Federated States of	691\n"
			+ "Midway Island	1\n"
			+ "Moldova	373\n"
			+ "Monaco	377\n"
			+ "Mongolia	976	001\n"
			+ "Montenegro	382\n"
			+ "Montserrat	1\n"
			+ "Morocco	212\n"
			+ "Mozambique	258\n"
			+ "Myanmar	95\n"
			+ "Namibia	264\n"
			+ "Nauru	674\n"
			+ "Nepal	977\n"
			+ "Netherlands	31\n"
			+ "Netherlands Antilles	599\n"
			+ "Nevis	1\n"
			+ "New Caledonia	687\n"
			+ "New Zealand	64\n"
			+ "Nicaragua	505\n"
			+ "Niger	227\n"
			+ "Nigeria	234	009\n"
			+ "Niue	683\n"
			+ "Norfolk Island	672\n"
			+ "Northern Mariana Islands	1\n"
			+ "Norway	47\n"
			+ "Oman	968\n"
			+ "Pakistan	92\n"
			+ "Palau	680\n"
			+ "Palestinian territories	970\n"
			+ "Panama	507\n"
			+ "Papua New Guinea	675\n"
			+ "Paraguay	595\n"
			+ "Peru	51\n"
			+ "Philippines	63\n"
			+ "Poland	48\n"
			+ "Portugal	351\n"
			+ "Puerto Rico	1\n"
			+ "Qatar	974\n"
			+ "R챕union	262\n"
			+ "Romania	40\n"
			+ "Russia	7	810|826|827|828|856|857|858|859|810\n"
			+ "Rwanda	250\n"
			+ "Saba	599\n"
			+ "Saint Barth챕lemy	590\n"
			+ "Saint Helena, Ascension and Tristan da Cunha	290\n"
			+ "Saint Kitts and Nevis	1\n"
			+ "Saint Lucia	1\n"
			+ "Saint Martin (French)	590\n"
			+ "Saint Pierre and Miquelon	508\n"
			+ "Saint Vincent and the Grenadines	1\n"
			+ "Samoa	685	0\n"
			+ "San Marino	378\n"
			+ "S찾o Tom챕 and Pr챠ncipe	239\n"
			+ "Saudi Arabia	966\n"
			+ "Senegal	221\n"
			+ "Serbia	381\n"
			+ "Seychelles	248\n"
			+ "Sierra Leone	232\n"
			+ "Singapore	65\n"
			+ "Sint Eustatius	599\n"
			+ "Sint Maarten (Dutch)	599\n"
			+ "Slovakia	421\n"
			+ "Slovenia	386\n"
			+ "Solomon Islands	677\n"
			+ "Somalia	252\n"
			+ "South Africa	27\n"
			+ "South Georgia and the South Sandwich Islands	500\n"
			+ "Spain	34\n"
			+ "Sri Lanka	94\n"
			+ "Sudan	249\n"
			+ "Suriname	597\n"
			+ "Svalbard and Jan Mayen	47\n"
			+ "Swaziland	268\n"
			+ "Sweden	46\n"
			+ "Switzerland	41\n"
			+ "Syria	963\n"
			+ "Taiwan	886	002|005|006|007|009\n"
			+ "Tajikistan	992	810\n"
			+ "Tanzania	255	000\n"
			+ "Thailand	66	001\n"
			+ "Thuraya (Mobile Satellite service)	882\n"
			+ "Togo	228\n"
			+ "Tokelau	690\n"
			+ "Tonga	676\n"
			+ "Trinidad and Tobago	1\n"
			+ "Tunisia	216\n"
			+ "Turkey	90\n"
			+ "Turkmenistan	993	810\n"
			+ "Turks and Caicos Islands	1\n"
			+ "Tuvalu	688\n"
			+ "Uganda	256	000\n"
			+ "Ukraine	380\n"
			+ "United Arab Emirates	971\n"
			+ "United Kingdom	44\n"
			+ "United States	1\n"
			+ "Universal Personal Telecommunications (UPT)	878\n"
			+ "Uruguay	598\n"
			+ "Uzbekistan	998	810\n"
			+ "Vanuatu	678\n"
			+ "Venezuela	58\n"
			+ "Vatican City State (Holy See)	379\n"
			+ "Vietnam	84\n"
			+ "Virgin Islands, British	1\n"
			+ "Virgin Islands, US	1\n"
			+ "Wake Island	1\n"
			+ "Wallis and Futuna	681\n"
			+ "Yemen	967\n"
			+ "Zambia	260\n"
			+ "Zanzibar	255\n"
			+ "Zimbabwe	263\n";
			
		
		this.setRules(input.getBytes());
		
	
	
	}

	public static PhoneNumberUtil getInstance() {
		if (instance == null) {
			instance = new PhoneNumberUtil();
		}

		return instance;
	}
	public void setRules(byte[] input)
	{
		CountryCodes = new ArrayList<String>();
		CountryCallingCodes = new ArrayList<String>();
		InternationalCallPrefix = new ArrayList<String>();
		CountryPattern = new ArrayList<Pattern>();
		DelStartZero = new ArrayList<Boolean>();
		
		
		
		String[] parsed = null;
		try {
			parsed = new String(input, "UTF-8").split("\n");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		
		for(int i=0; parsed != null && i < parsed.length ; i++)
		{
			String[] internalparsed = parsed[i].split("\t");
			CountryCodes.add(internalparsed[0].trim());
			CountryCallingCodes.add(internalparsed[1].trim());
			if(internalparsed.length > 2 && internalparsed[2] != null)
			{
				InternationalCallPrefix.add(internalparsed[2].trim());
			}
			else
			{
				InternationalCallPrefix.add("00");
			}
			
			if(internalparsed.length > 3 && internalparsed[3] != null)
			{
				if(internalparsed[3].trim().length() == 0)
				{
					CountryPattern.add(Pattern.compile("[0-9]{7,14}"));
				}
				else
				{
				CountryPattern.add(Pattern.compile(internalparsed[3].trim()));
				}
				
			}
			else
			{
				CountryPattern.add(Pattern.compile("[0-9]{7,14}"));
			}
			if(internalparsed.length > 4 && internalparsed[4] != null)
			{
				if("false".equalsIgnoreCase(internalparsed[4]))
				{
					DelStartZero.add(false);
				}
				
			}
			else
			{
				DelStartZero.add(true);
			}
		}
		
	}
	
	
	// http://ko.wikipedia.org/wiki/%EA%B5%AD%EC%A0%9C%EC%A0%84%ED%99%94_%EB%82%98%EB%9D%BC_%EB%B2%88%ED%98%B8
	// http://en.wikipedia.org/wiki/List_of_country_calling_codes
	// http://en.wikipedia.org/wiki/011
	
	
	/**
	 * format to global format with msisdn
	 * 
	 * @param phoneNumber
	 * @param msisdn
	 * @return
	 */
	
	public int toListidxByMsisdn(String msisdn)
	{
		int listidx = -1;
		
		if(msisdn == null)
		{
			return -1;
		}
		for(int i=0; CountryCallingCodes != null && i < CountryCallingCodes.size() ; i++)
		{
			
			if(msisdn.startsWith(CountryCallingCodes.get(i)))			
			{
				listidx = i;
				break;
			}
		}
		
		
		return listidx;
		
	}
	public String toGlobalFormatByListidx(String phoneNumber, int listidx, String orgnumber)
	{
		String rtn = phoneNumber.replaceAll("\\D", "");
		
		
		boolean hasPlusPrefix = false;
		if(phoneNumber.startsWith("+"))
		{
			hasPlusPrefix = true;
		}
			
		
		
		
		if(listidx== -1 && hasPlusPrefix == false)
		{
			return null;
		}
		if(CountryCallingCodes == null)
		{
			return null;
		}
		
		if(listidx != -1 && orgnumber != null)
		{
			Pattern icp = Pattern.compile("("+InternationalCallPrefix.get(listidx) + ")[0-9]{1,25}");
			Matcher icpm = icp.matcher(phoneNumber);
			if(icpm.matches())
			{
				rtn = phoneNumber.replaceFirst(InternationalCallPrefix.get(listidx), "");
	
				return rtn;
				
			}
		}
	
		if(hasPlusPrefix )
		{
			for(int i=0;  i < CountryCallingCodes.size() ; i++)
			{
				if(rtn.startsWith(CountryCallingCodes.get(i)))
				{
					String onlynumber = rtn.substring(CountryCallingCodes.get(i).length(), rtn.length());
					
					if(hasPlusPrefix && onlynumber.startsWith("0") && DelStartZero.get(i))
					{
						
						onlynumber = onlynumber.substring(1,onlynumber.length());
						rtn = CountryCallingCodes.get(i)+ onlynumber;
					}
					if(hasPlusPrefix && CountryCallingCodes.get(i).equals("54") && onlynumber.startsWith("9") == false)
					{
						rtn = CountryCallingCodes.get(i)+ "9"+onlynumber;
					}
					
					
					Matcher m = CountryPattern.get(i).matcher(onlynumber);
					
					if(m.matches())
					{
						return rtn;
					}				
				}
			}
		}
		
		
		
		if(DelStartZero.get(listidx) && rtn.startsWith("0"))
		{
			rtn = rtn.substring(1,rtn.length());
		}
		
		if(CountryCallingCodes.get(listidx).startsWith("54") && rtn.startsWith("15"))
		{
			rtn = rtn.substring(2,rtn.length());
			rtn = CountryCallingCodes.get(listidx) + rtn;
			return rtn;
		}
		if(CountryCallingCodes.get(listidx).startsWith("54") && rtn.startsWith("9")== false)
		{			
			rtn = "549" + rtn;
			return rtn;
		}
		if(CountryCallingCodes.get(listidx).startsWith("54") && rtn.startsWith("9")== true)
		{
			rtn = "54" + rtn;
			return rtn;
		}
		if(CountryCallingCodes.get(listidx).startsWith("56") && rtn.startsWith("9")== false)
		{			
			rtn = "569" + rtn;
			return rtn;
		}
		if(CountryCallingCodes.get(listidx).startsWith("56") && rtn.startsWith("9")== true)
		{
			rtn = "56" + rtn;
			return rtn;
		}
		if(CountryCallingCodes.get(listidx).startsWith("55"))
		{
			//rtn = "54" + rtn;
			//return rtn;
			if(rtn.length() == 8 && orgnumber != null)
			{
				rtn = "55" +  orgnumber.substring(2,4) + rtn ;
				return rtn;
			}
			else if(rtn.length() == 12)
			{
				rtn = "55" +rtn.substring(2, 12);
				return rtn;
			}
			
		}
		
		
		Matcher m = CountryPattern.get(listidx).matcher(rtn);
		if(m.matches())
		{
			rtn = CountryCallingCodes.get(listidx) + rtn;
			return rtn;
		}

		return null;	
	}
	public String toGlobalFormatByMsisdn(String phoneNumber, String msisdn) {
			
		int listidx = toListidxByMsisdn(msisdn);
		return this.toGlobalFormatByListidx(phoneNumber, listidx, msisdn);
	}
	public String toCountryCallingCodesByListidx(int idx)
	{
		return CountryCallingCodes.get(idx);
	}

	/**
	 * check is valid msisdn
	 * 
	 * @param msisdn
	 * @return
	 */
	
	public boolean isValidMsisdn(String msisdn) {
		Pattern re = Pattern.compile("^[1-9][0-9]{9,14}$");
		Matcher m = re.matcher(msisdn);
		
		if (m.matches())
			return true;
		else
			return false;
	}

	public boolean isValidDigit(String msisdn) {
		Pattern re = Pattern.compile("^[0-9]*$");
		Matcher m = re.matcher(msisdn);
		
		if (m.matches())
			return true;
		else
			return false;
	}
	
	public String convertToMsisdn(String phoneNumber, String countrycallingcode) {
		
		int listidx = toListidxByMsisdn(countrycallingcode);
		if(listidx == -1) {
			throw new CommonServiceException(HttpStatus.BAD_REQUEST, 
					ErrorMessage.fromFaultCode(ChatONOpenAPIFaultCode.AUTHENTICATE_COUNTRY_CALLING_CODE_IS_INVALID));
		}
		if(!isValidDigit(phoneNumber)) {
			throw new CommonServiceException(HttpStatus.BAD_REQUEST, 
					ErrorMessage.fromFaultCode(ChatONOpenAPIFaultCode.AUTHENTICATE_PHONENUMBER_IS_INVALID));
		}
		
		String msisdn = toGlobalFormatByListidx(phoneNumber, listidx, countrycallingcode);
		if(org.apache.commons.lang.StringUtils.isBlank(msisdn)) {
			throw new CommonServiceException(HttpStatus.BAD_REQUEST, 
					ErrorMessage.fromFaultCode(ChatONOpenAPIFaultCode.AUTHENTICATE_PHONENUMBER_IS_INVALID));
		}
		
		return msisdn;
	}
/*	
	@Test
	public void dfgdfgPhoneNumber()
	{
		PhoneNumberUtil pn = PhoneNumberUtil.getInstance();
		@SuppressWarnings("unused")
		int idx = pn.toListidxByMsisdn("+54");
		
		//System.out.println("CountryCallingCodes : " + pn.toCountryCallingCodesByListidx(idx));
		//String a = pn.toGlobalFormatByListidx("261029470252", idx);
		//String a = pn.toGlobalFormatByMsisdn("91158475550001", "549113855876624");
		String a = pn.toGlobalFormatByMsisdn("909038166292", "551154247400");
		System.out.println(a);
	}
	*/
}
